app.controller('ProductCtrl', ['$scope', '$timeout', '$http', '$compile', 'R', 'toaster', '$modal', '$rootScope', function($scope, $timeout, $http, $compile, R, toaster, $modal, $rootScope) {
    $scope.mappers = {
        classifyId: [{
            'id': -1,
            'name': '全部分类'
        }],
        address: [],
        keywordType: [{
            'key': 0,
            'value': '标题'
        },{
            'key': 1,
            'value': '标签'
        }]
    };

    R.getAllClassifyAndDirection().then(function(data){
        $scope.mappers.classifyId = $scope.mappers.classifyId.concat(data.bizData.classifys);
    });

    R.getAddressList().then(function(data){
        $scope.mappers.address = data.bizData;
    });

    $scope.model = {
        startTime: '',
        endTime: '',
        l1: null,
        l2: null,
        l3: null
    }

    $scope.$watch('model.l1', function(){
        $scope.model.l2 = null;
    });

    $scope.$watch('model.l2', function(){
        $scope.model.l3 = null;
    });

    $scope.currentpage = 20;
    $scope.totalCount = 0;

    $scope.query = {
        classifyId: -1,
        address: -1,
        startTime: -1,
        endTime: -1,
        checkState: -1,
        keyword: "",
        keywordType: 0,
        pageNo: 0,
        pageSize: $scope.currentpage
    }

    $scope.opened = [];
    $scope.open = function($event,index) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened[index] = true;
    };

    $scope.update = function(type){
        var res = [];
        angular.forEach($scope.rowCollectionPage, function(item){
            if (item.checked){
                res.push(item.id);
            }
        });

        if (!res.length) return;

        if (type == -1){
            $rootScope.confirm('确认删除该产品?', '提示信息')
            .then(function () {
                update(res, type);
            });
        }else{
            update(res, type);
        }
    }

    function update(res, type){
        R.batchUpdateProductState({
            ids: res.join(','),
            type: type
        }).then(function(){
            toaster.success('操作成功');
            $scope.getItems();
        });
    }

    $scope.checkall = false;

    $scope.checkAll = function(){
        angular.forEach($scope.rowCollectionPage, function(item){
            item.checked = $scope.checkall;
        });
    }

    $scope.submit = function(){
        if ($scope.model.startTime){
            $scope.query.startTime = R.getTime($scope.model.startTime);
        }else{
            $scope.query.startTime = -1;
        }
        if ($scope.model.endTime){
            $scope.query.endTime = R.getTime($scope.model.endTime);
        }else{
            $scope.query.endTime = -1;
        }
        if ($scope.model.l3){
            $scope.query.address = $scope.model.l3.name;
        }else{
            if ($scope.model.l2){
                $scope.query.address = $scope.model.l2.name;
            }else{
                if ($scope.model.l1){
                    $scope.query.address = $scope.model.l1.name;
                }else{
                    $scope.query.address = -1;
                }
            }
        }

        $scope.query.pageNo = 0;
        $scope.getItems();
    }

    $scope.del = function(item){
        $rootScope.confirm('确认删除该产品?', '提示信息')
        .then(function () {
            update([item.id], -1);
        });
    }

    $scope.rowCollectionPage = [];

    $scope.getItems = function(tableState, ctrl) {
        if (!$scope.stCtrl && ctrl) {
            $scope.stCtrl = ctrl;
        }

        if (!tableState && $scope.stCtrl) {
            $scope.stCtrl.pipe();
            return;
        }

        var pagination = tableState.pagination;

        $scope.query.pageNo = Math.ceil(pagination.start / $scope.currentpage);

        R.getProductsByCondition($scope.query).then(function(data) {
            $scope.rowCollectionPage = data.bizData.list;
            $scope.totalCount = data.bizData.count;
            $scope.pages = tableState.pagination.numberOfPages = Math.ceil(data.bizData.count / $scope.currentpage);
        });
    };
}]);